﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OppOrderRuleBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOppOrderRuleDA oppOrderRuleDA = DataAccess.CreateOppOrderRuleDA();
        private static readonly IOppOrderRuleDetailDA oppOrderRuleDetailDA = DataAccess.CreateOppOrderRuleDetailDA();
        private PubParasInfo pubParasInfo;

        public OppOrderRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public OppOrderRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OppOrderRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<OppOrderRuleInfo> oppOrderRuleInfos = new List<OppOrderRuleInfo>();
                OppOrderRuleInfo oppOrderRuleInfo = null;
                foreach (string id in IDS)
                {
                    oppOrderRuleInfo = oppOrderRuleDA.GetOppOrderRuleByID(dataBase, tran, id, this.dataBaseID[0]);
                    oppOrderRuleInfo.OppOrderRuleDetailInfos = oppOrderRuleDetailDA.GetOppOrderRuleDetailListByID(dataBase, tran, id, this.dataBaseID[0]);
                    oppOrderRuleInfos.Add(oppOrderRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                oppOrderRuleDetailDA.DeleteOppOrderRuleDetail(dataBase, tran, IDS, this.dataBaseID);
                oppOrderRuleDA.DeleteOppOrderRule(dataBase, tran, IDS, this.dataBaseID);
                for (int i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<OppOrderRuleInfo>(dataBase, tran, oppOrderRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "OPP_ORDER_RULE", this.dataBaseID[0]);
                    if ((oppOrderRuleInfos[i].OppOrderRuleDetailInfos != null) && (oppOrderRuleInfos[i].OppOrderRuleDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < oppOrderRuleInfos[i].OppOrderRuleDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<OppOrderRuleDetailInfo>(dataBase, tran, oppOrderRuleInfos[i].OppOrderRuleDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "OPP_ORDER_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return oppOrderRuleDA.GetAllOppOrderRule(this.dataBaseID[0]);
        }

        public OppOrderRuleInfo GetInfoByID(string oppOrderRuleID)
        {
            OppOrderRuleInfo oppOrderRuleInfo = oppOrderRuleDA.GetOppOrderRuleByID(oppOrderRuleID, this.dataBaseID[0]);
            if (oppOrderRuleInfo != null)
            {
                oppOrderRuleInfo.OppOrderRuleDetailInfos = oppOrderRuleDetailDA.GetOppOrderRuleDetailListByID(oppOrderRuleID, this.dataBaseID[0]);
            }
            return oppOrderRuleInfo;
        }

        public DataSet GetInfoByQueryEntity(OppOrderRuleQueryEntity oppOrderRuleQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(oppOrderRuleQuery.OppOrderRuleID))
            {
                sqlWhere.Add(" OPP_ORDER_RULE_ID " + oppOrderRuleQuery.OppOrderRuleID);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + oppOrderRuleQuery.Descr);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + oppOrderRuleQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleQuery.SystemCreated))
            {
                sqlWhere.Add(" SYSTEM_CREATED " + oppOrderRuleQuery.SystemCreated);
            }
            return oppOrderRuleDA.GetOppOrderRuleByQueryList(sqlWhere, oppOrderRuleQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string oppOrderRuleID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                OppOrderRuleInfo oppOrderRuleInfo = obj as OppOrderRuleInfo;
                if (oppOrderRuleDA.CheckOppOrderRuleIDUnique(dataBase, tran, oppOrderRuleInfo.OppOrderRuleID, this.dataBaseID[0]))
                {
                    throw new Exception("OppOrderRuleID:" + oppOrderRuleInfo.OppOrderRuleID + "已经存在,请重新输入!");
                }
                oppOrderRuleInfo.UpdatedBy = oppOrderRuleInfo.CreatedBy;
                oppOrderRuleInfo.CreatedDate = DateTime.Now;
                oppOrderRuleInfo.UpdatedDate = DateTime.Now;
                oppOrderRuleDA.InsertOppOrderRule(dataBase, tran, oppOrderRuleInfo, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<OppOrderRuleInfo>(dataBase, tran, null, oppOrderRuleInfo, AuditAction.Insert, this.pubParasInfo.UserID, "OPP_ORDER_RULE", this.dataBaseID[0]);
                oppOrderRuleID = oppOrderRuleInfo.OppOrderRuleID;
                if ((oppOrderRuleInfo.OppOrderRuleDetailInfos != null) && (oppOrderRuleInfo.OppOrderRuleDetailInfos.Count > 0))
                {
                    for (int i = 0; i < oppOrderRuleInfo.OppOrderRuleDetailInfos.Count; i++)
                    {
                        if (oppOrderRuleDetailDA.CheckOppOrderRuleDetailIDUnique(dataBase, tran, oppOrderRuleInfo.OppOrderRuleDetailInfos[i].OppOrderRuleID, oppOrderRuleInfo.OppOrderRuleDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            throw new Exception(string.Concat(new object[] { "OppOrderRuleID:", oppOrderRuleInfo.OppOrderRuleDetailInfos[i].OppOrderRuleID, ",LineID:", oppOrderRuleInfo.OppOrderRuleDetailInfos[i].LineID, ",已经存在,请重新输入!" }));
                        }
                        oppOrderRuleInfo.OppOrderRuleDetailInfos[i].CreatedBy = oppOrderRuleInfo.CreatedBy;
                        oppOrderRuleInfo.OppOrderRuleDetailInfos[i].UpdatedBy = oppOrderRuleInfo.UpdatedBy;
                        oppOrderRuleInfo.OppOrderRuleDetailInfos[i].CreatedDate = DateTime.Now;
                        oppOrderRuleInfo.OppOrderRuleDetailInfos[i].UpdatedDate = DateTime.Now;
                        oppOrderRuleDetailDA.InsertOppOrderRuleDetail(dataBase, tran, oppOrderRuleInfo.OppOrderRuleDetailInfos[i], this.dataBaseID);
                        auditDataBLL.InsertAuditData<OppOrderRuleDetailInfo>(dataBase, tran, null, oppOrderRuleInfo.OppOrderRuleDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "OPP_ORDER_RULE_DETAIL", this.dataBaseID[0]);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return oppOrderRuleID;
        }

        public string Update(object obj)
        {
            string oppOrderRuleID = string.Empty;
            try
            {
                OppOrderRuleInfo oppOrderRuleInfo = obj as OppOrderRuleInfo;
                oppOrderRuleInfo.UpdatedDate = DateTime.Now;
                oppOrderRuleInfo.UpdatedBy = this.pubParasInfo.UserID;
                OppOrderRuleInfo oldoppOrderRuleInfo = oppOrderRuleDA.GetOppOrderRuleByID(oppOrderRuleInfo.OppOrderRuleID, this.dataBaseID[0]);
                if (oppOrderRuleDA.UpdateOppOrderRule(oppOrderRuleInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OppOrderRuleInfo>(oldoppOrderRuleInfo, oppOrderRuleInfo, AuditAction.Update, this.pubParasInfo.UserID, "OPP_ORDER_RULE", this.dataBaseID[0]);
                    oppOrderRuleID = oppOrderRuleInfo.OppOrderRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return oppOrderRuleID;
        }
    }
}

